package studentmanage.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import studentmanage.DTO.GradeDTO;
import studentmanage.VO.GradesVO;
import studentmanage.entity.Grades;
import studentmanage.entity.Students;
import studentmanage.mapper.GradeMapper;
import studentmanage.mapper.StudentMapper;
import studentmanage.service.GradeService;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GradeServiceImpl implements GradeService {
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private StudentMapper studentMapper;

    public List<GradesVO> selectGrade(GradeDTO gradeDTO){
        List<Grades> gradesList= gradeMapper.selectGrade(gradeDTO);
        List<GradesVO> list = new ArrayList<>();
        for (Grades grades:gradesList) {
            //建一个gradeVO对象
            GradesVO gradesVO=GradesVO.builder().build();
            BeanUtils.copyProperties(grades,gradesVO);
            Students students= studentMapper.selectByStudentId(grades.getStudentId());
            gradesVO.setStudentName(students.getName());
            gradesVO.setClassName(students.getClassName());
            log.info("gradeVO{}",gradesVO);
            list.add(gradesVO);
        }
        log.info("list{}",list);
        return list;
    }

    public void addGrade(Grades grades){
        gradeMapper.addGrade(grades);
    }
    public void deleteGrade(long gradeId){
        gradeMapper.deleteGrade(gradeId);
    }
    public void updateGrade(Grades grades){
        gradeMapper.updateGrade(grades);
    }
}
